//index.js
//获取应用实例
// const aliyunVod = require('../../utils/aliyun-upload-wx-sdk-1.0.0.js')
const test = require('../../utils/Tools.js')
const app = getApp()
const base64 = require('../../utils/base64.js');//Base64,hmac,sha1,crypto相关算法
require('../../utils/hmac.js');
require('../../utils/sha1.js');
const Crypto = require('../../utils/crypto.js');
const BASE64 = require('../../utils/base64.js');//Base64,hmac,sha1,crypto相关算法
const MD5 = require('../../utils/md5.js');//Base64,hmac,sha1,crypto相关算法
const Ajax = require('../../utils/AjaxWX.js').Ajax;//Base64,hmac,sha1,crypto相关算法


const uploadFile = require('../../utils/uploadFile.js'); //配置文件，在这文件里配置你的OSS keyId和KeySecret,timeout:87600;

const Tools = require('../../utils/Tools.js'); //配置文件，在这文件里配置你的OSS keyId和KeySecret,timeout:87600;

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        videoData:null,
        msg:""
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad: function () {
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse){
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
    getUserInfo: function(e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    uploadAuth: function (e) {
        var wx_this=this;
        wx_this.setData({msg:"信息1"});
        var s = null;




        console.log("s=",s)
        console.log("wx=",wx)

        wx.chooseVideo({
            sourceType: ['album', 'camera'],
            maxDuration: 60,
            camera: ['front', 'back'],
            success(res) {
                console.log("res=",res)
                wx_this.setData({msg:JSON.stringify(res)});
                var fileName=Tools.getMd5FileName(res.tempFilePath);
                var file={
                    wxTempFilePath:res.tempFilePath,
                    name:fileName,
                    size:res.size,
                    lastModified:Math.round(new Date()),

                }
                console.log("file=,",file);
                wx.getSavedFileInfo({filePath:file.wxTempFilePath,success:function (res) {
                        console.log("getSavedFileInfo.res=",res)
                        // wx_this.setData({msg:JSON.stringify(res)});
                    }});


                var createUrl = 'https://demo-vod.cn-shanghai.aliyuncs.com/voddemo/CreateUploadVideo?Title=testvod1&FileName=aa.mp4&BusinessType=vodai&TerminalType=pc&DeviceModel=iPhone9,2&UUID=59ECA-4193-4695-94DD-7E1247288&AppVersion=1.0.0&VideoId=5bfcc7864fc14b96972842172207c9e6'

                wx_this.setData({msg:"createUrl="+createUrl});

                try {
                    Ajax.get( createUrl, function (data) {
                        wx_this.setData({msg:"ajax.get"+data});
                        console.log("data=", data);
                        wx_this.setData({msg:JSON.stringify(data)});
                        console.log("createUrl.data=",data)
                        var uploadAuth = data.UploadAuth
                        var uploadAddress = data.UploadAddress
                        var videoId = data.VideoId

                        let address = JSON.parse(BASE64.decode(uploadAddress));
                        let auth = JSON.parse(BASE64.decode(uploadAuth));

                        var options={
                            //{
                            // "Endpoint":"https://oss-cn-shanghai.aliyuncs.com",
                            // "Bucket":"out-20170320144514766-asatv1s154",
                            // "FileName":"customerTrans/f847b24d5fb46895c3a2c7d3f3e58f13/40b79f6c-16a15773039-0004-a1f7-953-f8a30.mp4
                            // }
                            uploadAddress:address,
                            //{
                            // "SecurityToken":"CAIS1QR1q6Ft5B2yfSjIr4ngf/fip5VP5Y6CNkiIk0dhQexE3IbAiDz2IHpKeXduAeAXs/o0mmhZ7/YYlrUqEMceFByYM5AhvswJqVv5JpfZv8u84YADi5CjQeV2+NsymZ28Wf7waf+AUBDGCTmd5M4Yo9bTcTGlQCZuW//toJV7b9MRcxClZD5dfrl/LRdjr8lo1xGzUPG2KUzSn3b3BkhlsRYe72Rk8vaHxdaAzRDcgVbmqJcSvJ+jC4C8Ys9gG519XtypvopxbbGT8CNZ5z9A9qp9kM49/izc7P6QH35b4RiNL8/Z7tQNXwhiffobHa9YrfHgmNhlvvDSj43t1ytVOeZcX0akQ5u7ku7ZHP+oLt8jaYvjP3PE3rLpMYLu4T48ZXUSODtDYcZDUHhrEk4RUjXdI6Of8UrWSQC7Wsr217otg7Fyyk3s8MaHAkWLX7SB2DwEB4c4aEokVW4RxnezW6UBaRBpbld7Bq6cV5lOdBRZoK+KzQrJTX9Ez2pLmuD6e/LOs7oDVJ37WZtKyuh4Y49d4U8rVEjPQqiykT0mFgpfTK1RzbPmNLKm9baB25/zW+PdDe0dsVgoJ1KFpiGWG3RLNn+ztJ9xbkeEvp6JlfOUqMk4SVdyu4kDUl/bNN92pkB4urDrskjU+/L4Tnev83AE4NDe5M4NyzM9Lqj/3bDC42aA5SbMOfRmoqqNBj0zGybQUmdi3fWfikgAox08iGyuM34dkQ/LizvuIpVLg6HVnS0cU/4Dw/6kYjOh73hmN9WF2L8CQth+Z9pJVvusLK+eAAkMbXYagAEUA9OWgUFDJw+qhXWeBrfIFA2uZvo5AkXGD06V1gDieykaom5ygpjwp4ON1r9KHgMiAJsQyNdvHM8XcyCTLZKXflfZHv6B6ccU2LFbFbgvURCjc1k6YaYG7TdK2ICmTdSNh7hLS//rnnq20TVXFN6adDd1DH4etLUpKt1ZswPG4A==",
                            // "AccessKeyId":"STS.NJU4MVJJnRLi4n9wG4Mch3Ckj",
                            // "ExpireUTCTime":"2019-03-19T09:24:00Z",
                            // "AccessKeySecret":"6rJQ3ghwrK8bZZpmVW1oTzH8iioyvAL8F3RHEBRp956z",
                            // "Expiration":"3431",
                            // "Region":"cn-shanghai"
                            // }
                            uploadAuth:auth,
                            videoId:videoId,
                            file: file
                        }
                        console.log(JSON.stringify(options))
                        wx_this.setData({msg:JSON.stringify(options)});
                        uploadFile(options,function(res) {
                            console.log("res=",res)
                            wx_this.setData({msg:JSON.stringify(res)});
                        })

                    }, 'json')
                } catch(error) {
                    wx_this.setData({msg:JSON.stringify(error)});
                } finally {
                }
            }
        })

    },

})
